package controller;

import dao.ReservationDAO;
import model.Reservation;
import model.AttendanceRecord;
import java.util.List;

/**
 * 预约控制器
 */
public class ReservationController {
    private ReservationDAO reservationDAO;
    private AttendanceController attendanceController;

    public ReservationController() {
        this.reservationDAO = new ReservationDAO();
        this.attendanceController = new AttendanceController();
    }

    /**
     * 创建预约
     */
    public ReservationResult makeReservation(Reservation reservation) {
        try {
            // 防止占座：检查用户在同一时间段是否已有预约
            if (reservationDAO.hasUserTimeSlotConflict(reservation)) {
                return new ReservationResult(false, "同一时间段您已有预约，无法重复预约座位");
            }

            // 检查座位时间冲突
            if (reservationDAO.hasTimeConflict(reservation)) {
                return new ReservationResult(false, "该座位在选定时间段已被预约");
            }

            // 创建预约
            boolean reservationSuccess = reservationDAO.insertReservation(reservation);
            if (reservationSuccess) {
                // 预约成功后，创建对应的考勤记录
                AttendanceRecord attendanceRecord = new AttendanceRecord(
                        reservation.getReservationId(),
                        reservation.getUserId(),
                        reservation.getSeatId(),
                        reservation.getReservationDate(),
                        reservation.getStartTime(),
                        reservation.getEndTime());

                try {
                    attendanceController.createAttendanceRecord(attendanceRecord);
                } catch (Exception e) {
                    System.err.println("创建考勤记录失败，但预约已成功: " + e.getMessage());
                    // 预约已经成功，考勤记录创建失败不影响预约的成功
                }

                return new ReservationResult(true, "预约成功");
            }

            return new ReservationResult(false, "预约失败，请稍后重试");
        } catch (Exception e) {
            e.printStackTrace();
            return new ReservationResult(false, "预约失败: " + e.getMessage());
        }
    }

    /**
     * 取消预约
     */
    public boolean cancelReservation(int reservationId) {
        try {
            return reservationDAO.cancelReservation(reservationId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("取消预约失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户的预约记录
     */
    public List<Reservation> getUserReservations(int userId) {
        try {
            return reservationDAO.getUserReservations(userId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取预约记录失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有预约记录
     */
    public List<Reservation> getAllReservations() {
        try {
            return reservationDAO.getAllReservations();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取预约记录失败: " + e.getMessage());
        }
    }

    /**
     * 根据筛选条件获取预约记录
     */
    public List<Reservation> getFilteredReservations(String dateFilter, String statusFilter) {
        try {
            return reservationDAO.getFilteredReservations(dateFilter, statusFilter);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取预约记录失败: " + e.getMessage());
        }
    }

    /**
     * 搜索预约记录
     */
    public List<Reservation> searchReservations(String keyword) {
        try {
            return reservationDAO.searchReservations(keyword);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("搜索预约记录失败: " + e.getMessage());
        }
    }

    /**
     * 获取指定日期的预约记录
     */
    public List<Reservation> getReservationsByDate(String date) {
        try {
            return reservationDAO.getReservationsByDate(date);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取预约记录失败: " + e.getMessage());
        }
    }

    /**
     * 检查预约时间冲突
     */
    public boolean hasTimeConflict(Reservation reservation) {
        try {
            return reservationDAO.hasTimeConflict(reservation);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("检查时间冲突失败: " + e.getMessage());
        }
    }

    /**
     * 预约结果类
     */
    public static class ReservationResult {
        private boolean success;
        private String message;

        public ReservationResult(boolean success, String message) {
            this.success = success;
            this.message = message;
        }

        public boolean isSuccess() {
            return success;
        }

        public String getMessage() {
            return message;
        }
    }
}
